<?php
	#-------------------------------------------------------------#
	# This file is part of the Kumomado - Virtual Window Platform #
	# for PHP. (c) 2009 Tsai, Min Che <tirme0812@gmail.com>       #
	# Please visit http://www.kumomado.org/ for more information. #
	#                                                             #
	# This software is licensed under the GNU License v3. Please  #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#

    /**
	 * Core of Kumomado
     * @package framework/
	 * @author Tsai, Min Che
	 * @version 1.0
     */
	class Kumomado {
		/**
		 * @var object
		 * @link KumomadoLibrary
		 */
		protected $library = null;
		/**
		 * @var string
		 */
		public $project_name = 'Kumomado';
		/**
		 * @var string
		 */
		public $version = '4.3';
		/**
		 * Construct
		 */
		private function __construct() {
			// check root path is defined.
			if (!defined('KUMOMADO_ROOT_PATH')) {
				throw new Exception('The "KUMOMADO_ROOT_PATH" is not defined.');
			}
			
			// load required classes
			include_once KUMOMADO_FRAMEWORK_PATH.'KumomadoFunction.php';
			include_once KUMOMADO_EXCEPTIONS_PATH.'KumomadoException.class.php';
			include_once KUMOMADO_LIBRARIES_PATH.'KumomadoLibrary.class.php';
			include_once KUMOMADO_LIBRARIES_PATH.'KumomadoLoader.class.php';
			
			// init library
			$this->library = new KumomadoLibrary();
		}
		/**
		 * Access the only instance of this class
		 * @return object @link Kumomado
		 */
		public static function getInstance() {
			static $kumomado = NULL;
			if ($kumomado === NULL) {
				$class = __CLASS__;
				$kumomado = new $class();
			}
			return $kumomado;
		}
		public static function loadCore() {
			Kumomado::load('Logger.KumomadoLogger');
			Kumomado::load('Language.KumomadoLanguage');
			Kumomado::load('Config.KumomadoConfig');
			Kumomado::load('Xml.KumomadoXsltProcessor');
			Kumomado::load('Object.KumomadoObject');
			Kumomado::load('Object.KumomadoObjectHandler');
			Kumomado::load('Route.KumomadoRoute');
		}
		public static function run() {
			static $has_run = false;
			$exception = '';
			if (false == $has_run) {
				try {
					ob_start();
					self::loadCore();
					Kumomado::logger();
					$route = new KumomadoRoute();
					$route->execute();
					$main = ob_get_contents();
					ob_end_clean();
					//throw new KumomadoApplicationException('error');
					//throw new Exception('a');
				} catch (KumomadoRouteException $e) {
					$exception = 'Kumomado Route Exception: '.$e->getMessage();
				} catch (KumomadoLibraryException $e) {
					$exception = 'Kumomado Library Exception: '.$e->getMessage();
				} catch (KumomadoApplicationException $e) {
					$exception = $e->__toArray();
				} catch (KumomadoException $e) {
					$exception = 'Kumomado Exception: '.$e->getMessage();
				} catch (Exception $e) {
					$exception = 'Exception: '.$e->getMessage();
				}
				if ($exception == '') {
					echo $main;
				} else {
					echo json_encode(array('Exception' => $exception));
				}
			}
		}
		/**
		 * @param string $class
		 */
		public static function load($class) {
			$kumomado = Kumomado::getInstance();
			$kumomado->library->load($class);
		}
		/**
		 * Load database driver
		 * @param string $name The database config's name
		 * @return object
		 */
		public static function db($name = null) {
			$kumomado = Kumomado::getInstance();
			return $kumomado->library->db($name);
		}
		/**
		 * Load language definition
		 * @param string $source_file[option]
		 * @param string $source_package[option]
		 * @param string $language[option]
		 * @return object @link KumomadoLanguage
		 */
		public static function lang($source_file = '', $source_package = '', $language = null) {
			$kumomado = Kumomado::getInstance();
			return $kumomado->library->lang($source_file, $source_package, $language);
		}
		/**
		 *
		 * @return array
		 */
		public static function config($name = null) {
			$kumomado = Kumomado::getInstance();
			return $kumomado->library->config($name);
		}
		public static function logger() {
			$kumomado = Kumomado::getInstance();
			return $kumomado->library->logger();
		}
		/**
		 * Because of the singleton nature of this class, it cannot be cloned.
		 */
		private function __clone() {}
	}
?>